package org.lex.ftp.ui.bookmark;

import org.lex.ftp.util.bookmark.Bookmark;
import org.lex.ftp.util.bookmark.Site;
import org.lex.swing.tree.LazyMutableTreeNode;


public class BookmarkTreeNode extends LazyMutableTreeNode {
	private Bookmark folder = null;
	private Site site = null;

	public BookmarkTreeNode(Bookmark bookmark) {
		super(bookmark);
		this.folder = bookmark;
	}

	public BookmarkTreeNode(Site site) {
		super(site);
		this.site = site;
	}

	public String getName() {
		if (isFolder())
			return this.folder.getName();
		else if (isSite())
			return site.getName();
		else
			return "(error)";
	}

	public boolean isFolder() {
		return null != this.folder;
	}

	public Bookmark getFolder() {
		return this.folder;
	}

	public boolean isSite() {
		return null != this.site;
	}

	public Site getSite() {
		return site;
	}

	@Override
	public void setUserObject(Object userObject) {
		super.setUserObject(userObject);
		if (this.isFolder())
			this.folder.setName(String.valueOf(userObject));
		if (this.isSite())
			this.site.setName(String.valueOf(userObject));
	}

	@Override
	protected void loadChildren() {
		if (null == folder)
			return;
		// first add folders
		for (int i = 0, n = folder.getFolderCount(); i < n; i++)
			this.add(new BookmarkTreeNode(folder.getFolder(i)));
		// then add sites
		for (int i = 0, n = folder.getSiteCount(); i < n; i++)
			this.add(new BookmarkTreeNode(folder.getSite(i)));
	}
}
